如何在Android中解析XML文件?

发布于 2025-02-11 18:07:46 字数 382 浏览 1 评论 0原文

我有一个XML文件,例如

<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
   <rootfiles>
        <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
   </rootfiles>
</container>

我需要全路径值,即“ oebps/content.opf”文本。我尝试使用文档构建器和XML解析器,但没有结果。我如何穿越该节点并获得值

I have an XML file like

<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
   <rootfiles>
        <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
   </rootfiles>
</container>

I need the full-path value that is "OEBPS/content.opf" text. I tried using Document builder and XML parser but no results. How do I traverse to that node and get the value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2025-02-18 18:07:46

我找到了一种访问属性值的方法。我已经解释了评论中的所有行。

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = factory.newDocumentBuilder();

 Document doc = builder.parse(file);   // We can use inputstream or uri also

 NodeList node = doc.getElementsByTagName("rootfile"); //  gets list of all nodes by specified tag name.

 Node map = node.item(0); // Getting the specific node

 NamedNodeMap namedNodeMap = map.getAttributes();  // Getting attributed of the nodes.
            
 Node file_path = namedNodeMap.getNamedItem("full-path"); // getting the partcular node attribute

 String path = file_path.getNodeValue();  // This will give the value of full-path 

I found a way to access the attribute values. I have explained all the lines in the comments.

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = factory.newDocumentBuilder();

 Document doc = builder.parse(file);   // We can use inputstream or uri also

 NodeList node = doc.getElementsByTagName("rootfile"); //  gets list of all nodes by specified tag name.

 Node map = node.item(0); // Getting the specific node

 NamedNodeMap namedNodeMap = map.getAttributes();  // Getting attributed of the nodes.
            
 Node file_path = namedNodeMap.getNamedItem("full-path"); // getting the partcular node attribute

 String path = file_path.getNodeValue();  // This will give the value of full-path 
想你的星星会说话 2025-02-18 18:07:46

我已经使用了 jackson serialize serialize and deSerialize(parse)xml。我为每个元素制作一个pojo,然后根据需要使用其解析的注释。以下是一个粗略的例子。但是,这两个外部类将使用@jsonignoreproperties注释忽略所有内容(除非您将其包含在POJO中)。 rootfile类将使用@jacksonxmlproperty注释来解析您想要的属性。

Sidenote:我不是@jsonrootname的100%。这就是我标记XML root的方式,但是我的同事使用@jackonxmlrootlement(value =“ ...”)将XML字符串解析为POJO>

@JsonRootName(value = "container")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Container {

  @JsonIgnoreProperties(ignoreUnknown = true)
  public class Rootfiles {

    @JsonRootName(value = "rootfile")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Rootfile {
      
      @JacksonXmlProperty(isAttribute = true, localName = "full-path")
      private String fullPath;

      public String getFullPath() { return fullPath; }
    }
  }
}

I've used Jackson to serialize and deserialize(parse) xml. I make a POJO for each element and then using the annotations it parses to the POJO as needed. The below is a rough example. But the the two outer classes will ignore everything(unless you include it in the POJO) using the @JsonIgnoreProperties annotations. And the Rootfile class will parse the attribute you want using the @JacksonXmlProperty annotation.

Sidenote: I'm not 100% on the @JsonRootName. That is how I marked the xml root, but my colleague parsed the xml string to POJO using @JackonXmlRootElement(value = "...")

@JsonRootName(value = "container")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Container {

  @JsonIgnoreProperties(ignoreUnknown = true)
  public class Rootfiles {

    @JsonRootName(value = "rootfile")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Rootfile {
      
      @JacksonXmlProperty(isAttribute = true, localName = "full-path")
      private String fullPath;

      public String getFullPath() { return fullPath; }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文