Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath 使用Java解析DOM4j中具有自己名称空间的子节点_Xpath_Namespaces_Dom4j - Fatal编程技术网

Xpath 使用Java解析DOM4j中具有自己名称空间的子节点

Xpath 使用Java解析DOM4j中具有自己名称空间的子节点,xpath,namespaces,dom4j,Xpath,Namespaces,Dom4j,我希望有人能帮忙修理我的食物吧。我已经在DOM4j解析器上工作了大约一个月,使用XPATH从XML文件中提取了500多个数据元素。不幸的是,我使用一个旧的测试文件作为模型来创建代码,只有在插入生产文件后才发现错误。下面是我的代码的一个小示例。从Hashmap中可以看到,完整XML中使用了几个名称空间。我将代码缩减为只提取3个元素 import java.io.File; import java.util.HashMap; import java.util.Map; import org.dom

我希望有人能帮忙修理我的食物吧。我已经在DOM4j解析器上工作了大约一个月,使用XPATH从XML文件中提取了500多个数据元素。不幸的是,我使用一个旧的测试文件作为模型来创建代码,只有在插入生产文件后才发现错误。下面是我的代码的一个小示例。从Hashmap中可以看到,完整XML中使用了几个名称空间。我将代码缩减为只提取3个元素

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class CLOBTest {

public static void main(String[] args) {
      try {
         File inputFile = new File("C:/test.xml");
         //File inputFile = new File("C:/test1.xml");
         SAXReader reader = new SAXReader();
         Document document = reader.read( inputFile );

         Map<String, String> map = new HashMap<String, String>();

         map.put("exch", "http://at.dsh.cms.gov/exchange/1.0");
         map.put("ext", "http://at.dsh.cms.gov/extension/1.0");
         map.put("hix-core", "http://hix.cms.gov/0.1/hix-core");
         map.put("hix-ee", "http://hix.cms.gov/0.1/hix-ee");
         map.put("hix-pm", "http://hix.cms.gov/0.1/hix-pm");
         map.put("nc", "http://niem.gov/niem/niem-core/2.0");
         map.put("niem-core", "http://niem.gov/niem/niem-core/2.0");
         map.put("s", "http://niem.gov/niem/structures/2.0");
         map.put("scr", "http://niem.gov/niem/domains/screening/2.1");
         map.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");


         XPath Request = DocumentHelper.createXPath("//exch:AccountTransferRequest");
         Request.setNamespaceURIs(map);

         Node request =  Request.selectSingleNode(document);

         System.out.println("  ID:        \t" + request.valueOf("ext:TransferHeader/ext:TransferActivity/niem-core:ActivityIdentification/niem-core:IdentificationID")); 
         System.out.println("  First Name:\t" + request.valueOf("hix-core:Person/niem-core:PersonName/niem-core:PersonGivenName")); 
         System.out.println("  Last Name: \t" + request.valueOf("hix-core:Person/niem-core:PersonName/niem-core:PersonSurName")); 

      } catch (DocumentException e) {
         e.printStackTrace();
      }
   }
}
test.xml

 <H15>
 <requestMSG>
 <exch:AccountTransferRequest xmlns:exch="http://at.dsh.cms.gov/exchange/1.0" xmlns:hix-core="http://hix.cms.gov/0.1/hix-core" xmlns:niem-core="http://niem.gov/niem/niem-core/2.0" xmlns:s="http://niem.gov/niem/structures/2.0" xmlns:ext="http://at.dsh.cms.gov/extension/1.0" ext:atVersionText="2.3">
 <ext:TransferHeader>
 <ext:TransferActivity>
 <niem-core:ActivityIdentification xmlns:niem-core="http://niem.gov/niem/niem-core/2.0">
 <niem-core:IdentificationID>XXX012345</niem-core:IdentificationID>
 </niem-core:ActivityIdentification>
 </ext:TransferActivity>
 </ext:TransferHeader>
 <hix-core:Person xmlns:hix-core="http://hix.cms.gov/0.1/hix-core" xmlns:s="http://niem.gov/niem/structures/2.0" s:id="Mom">
 <niem-core:PersonName xmlns:niem-core="http://niem.gov/niem/niem-core/2.0">
 <niem-core:PersonGivenName>gina</niem-core:PersonGivenName>
 <niem-core:PersonSurName>davis</niem-core:PersonSurName>
 </niem-core:PersonName>
 </hix-core:Person>
 </exch:AccountTransferRequest>
 </requestMSG>
 </H15>

现在,如何提取具有自己名称空间的子节点的值?是否有一种方法可以在仍然通过请求节点时执行此操作?如果可能的话,我想挽回一些努力。

看起来您需要进一步创建
XPath
对象,每次都设置名称空间上下文,例如

     XPath Request = DocumentHelper.createXPath("//exch:AccountTransferRequest");
     Request.setNamespaceURIs(map);

     Node request =  Request.selectSingleNode(document);

     XPath idRequest = DocumentHelper.createXPath("ext:TransferHeader/ext:TransferActivity/niem-core:ActivityIdentification/niem-core:IdentificationID");
     idRequest.setNamespaceURIs(map);

     System.out.println("  ID:        \t" + idRequest.selectSingleNode(request).getText()); 

好吧,算了。只需将请求节点强制转换为一个元素并添加所有名称空间。一旦我在输出中使用了元素,整个文档就会识别它们

         Element test = (Element) request;
         test.addNamespace("exch", "http://at.dsh.cms.gov/exchange/1.0");
         test.addNamespace("ext", "http://at.dsh.cms.gov/extension/1.0");
         test.addNamespace("hix-core", "http://hix.cms.gov/0.1/hix-core");
         test.addNamespace("hix-ee", "http://hix.cms.gov/0.1/hix-ee");
         test.addNamespace("hix-pm", "http://hix.cms.gov/0.1/hix-pm");
         test.addNamespace("nc", "http://niem.gov/niem/niem-core/2.0");
         test.addNamespace("niem-core", "http://niem.gov/niem/niem-core/2.0");
         test.addNamespace("s", "http://niem.gov/niem/structures/2.0");
         test.addNamespace("scr", "http://niem.gov/niem/domains/screening/2.1");
         test.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

         System.out.println("  ID:                                             \t"+test.valueOf("ext:TransferHeader/ext:TransferActivity/niem-core:ActivityIdentification/niem-core:IdentificationID")); 

好极了这应该可以让我挽救很多,但仍将是大量的重新编码。我会让你知道结果的。非常感谢。我已经试了一个多星期了。我上过几节课,但现在遇到了困难。如果XPATH在一个节点中启动,但引用了另一个节点,则无法提前绑定第二个节点的名称空间。是否有方法从XML文档中选择一个节点,向其添加所有可能的名称空间属性,并将其作为节点传递给另一个仍然附加名称空间属性的类?我需要从节点转换到元素还是从节点转换到元素?
Exception in thread "main" org.dom4j.XPathException: Exception occurred evaluting XPath: ext:TransferHeader/ext:TransferActivity/niem-core:ActivityIdentification/niem-core:IdentificationID. Exception: XPath expression uses unbound namespace prefix niem-core
    at org.dom4j.xpath.DefaultXPath.handleJaxenException(DefaultXPath.java:374)
    at org.dom4j.xpath.DefaultXPath.valueOf(DefaultXPath.java:185)
    at org.dom4j.tree.AbstractNode.valueOf(AbstractNode.java:191)
    at CLOBTest.main(CLOBTest.java:41)
     XPath Request = DocumentHelper.createXPath("//exch:AccountTransferRequest");
     Request.setNamespaceURIs(map);

     Node request =  Request.selectSingleNode(document);

     XPath idRequest = DocumentHelper.createXPath("ext:TransferHeader/ext:TransferActivity/niem-core:ActivityIdentification/niem-core:IdentificationID");
     idRequest.setNamespaceURIs(map);

     System.out.println("  ID:        \t" + idRequest.selectSingleNode(request).getText()); 
         Element test = (Element) request;
         test.addNamespace("exch", "http://at.dsh.cms.gov/exchange/1.0");
         test.addNamespace("ext", "http://at.dsh.cms.gov/extension/1.0");
         test.addNamespace("hix-core", "http://hix.cms.gov/0.1/hix-core");
         test.addNamespace("hix-ee", "http://hix.cms.gov/0.1/hix-ee");
         test.addNamespace("hix-pm", "http://hix.cms.gov/0.1/hix-pm");
         test.addNamespace("nc", "http://niem.gov/niem/niem-core/2.0");
         test.addNamespace("niem-core", "http://niem.gov/niem/niem-core/2.0");
         test.addNamespace("s", "http://niem.gov/niem/structures/2.0");
         test.addNamespace("scr", "http://niem.gov/niem/domains/screening/2.1");
         test.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

         System.out.println("  ID:                                             \t"+test.valueOf("ext:TransferHeader/ext:TransferActivity/niem-core:ActivityIdentification/niem-core:IdentificationID"));