问题描述
public class TestXml { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // never forget this! DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("d://dd.xml"); XPathFactory pathFactory = XPathFactory.newInstance(); XPath xpath = pathFactory.newXPath(); XPathExpression pathExpression = xpath.compile("//Model/o:Column/a:Name/text()"); Object result = pathExpression.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } } } dd.xml<?xml version="1.0" encoding="GB2312"?><?PowerDesigner AppLocale="UTF16" ID="{64EF704A-101D-4EF0-B2F3-F2586373698A}" Label="" Name="rank" Objects="203" Symbols="12"ID="{64EF704A-101D-4EF0-B2F3-F2586373698A}" Label="" Name="rank" Objects="203" Symbols="12" Target="ORACLE Version 10g" Type="{CDE44E21-9669-11D1-9914-006097355D9B}" signature="PDM_DATA_MODEL_XML"version="12.0.0.1700"?><Model xmlns:a="attribute" xmlns:c="collection" xmlns:o="object"><o:Column Id="o29"><a:ObjectID>627887B7-D8B5-4151-936A-15A527C092B1</a:ObjectID><a:Name>排行控制表ID</a:Name><a:Code>rank_control_id</a:Code><a:CreationDate>1269244973</a:CreationDate><a:Creator>Administrator</a:Creator><a:ModificationDate>1269259813</a:ModificationDate><a:Modifier>Administrator</a:Modifier><a:Comment>排行控制表主键</a:Comment><a:DataType>NUMBER(19)</a:DataType><a:Length>19</a:Length><a:Mandatory>1</a:Mandatory></o:Column></Model>这样为什么不能读取?但是下面这个不要命名空间的可以读取<?xml version="1.0" encoding="GB2312"?><?PowerDesigner AppLocale="UTF16" ID="{64EF704A-101D-4EF0-B2F3-F2586373698A}" Label="" Name="rank" Objects="203" Symbols="12"ID="{64EF704A-101D-4EF0-B2F3-F2586373698A}" Label="" Name="rank" Objects="203" Symbols="12" Target="ORACLE Version 10g" Type="{CDE44E21-9669-11D1-9914-006097355D9B}" signature="PDM_DATA_MODEL_XML"version="12.0.0.1700"?><Model><Column Id="o29"><ObjectID>627887B7-D8B5-4151-936A-15A527C092B1</ObjectID><Name>排行控制表ID</Name><Code>rank_control_id</Code><CreationDate>1269244973</CreationDate><Creator>Administrator</Creator><ModificationDate>1269259813</ModificationDate><Modifier>Administrator</Modifier><Comment>排行控制表主键</Comment><DataType>NUMBER(19)</DataType><Length>19</Length><Mandatory>1</Mandatory></Column></Model> 问题补充:7454103 写道
解决方案
引用试试去解析一下pdm文件里面的表的字段的信息。我现在就在搞这个,很难解析啊 难怪 PowerDesigner 的 看你的XML 咋那么别扭了! 好的我试试!
解决方案二:
用用我的方法 绝对管用
解决方案三:
public static void main(String[] args) {File f = new File("WebRoot/swdqyxxadd.xml");Document doc = null;DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();try {DocumentBuilder db = dbf.newDocumentBuilder();try {// doc = db.parse(f);//用文件形式来读取数据String str = "<?xml version='1.0' encoding='gb2312'?>" +"<CorpInfos>" +"<CorpInfo>" +"<GUID>0A23B90600000110BA538FC800000096</GUID><!-- 唯一标识,南大不需要关心 -->" +"<YYZZZCH>991991</YYZZZCH><!-- 营业执照注册号 -->" +"<ZZJGDM>123123</ZZJGDM><!-- 组织机构代码 -->" +"</CorpInfo>" +"</CorpInfos>";InputStream is = new ByteArrayInputStream(str.getBytes());//将字符串改成流的形式doc = db.parse(is);//以流的形式读取} catch (IOException e) {System.out.println("没找到此文件");}} catch (Exception e) {e.printStackTrace();}NodeList list = doc.getElementsByTagName("CorpInfo");System.out.println(doc.getElementsByTagName("GUID").item(0).getFirstChild().getNodeValue());//Element element = null;//System.out.println(list.getLength());//for (int i = 0; i < list.getLength(); i++) {//element = (Element) list.item(i);// NodeList guid = element.getElementsByTagName("GUID");// System.out.println(guid.item(0).getFirstChild().getNodeValue());////System.out.println(element.getElementsByTagName("YYZZZCH").item(i)////.getFirstChild().getNodeValue());//}}
解决方案四:
XML 经常用! 解析的也不少! 没解析过加命名空间的! 关,注下能否补充下! 有错误信息没?
解决方案五:
xmlns:a="attribute" xmlns:c="collection" xmlns:o="object"可能是这个命名空间不规范 或者 错了吧!