Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

java - Need to extract element from XML

Below is the XML:-

 <?xml version="1.0" encoding="utf-8"?>
<Stock>
    <Identification>
      <AccountID></AccountID>
      <CustomerId></CustomerId>
    </Identification>
 <Product>
    <ArticleName>Monitors</ArticleName>
    <BaseUnit></BaseUnit>
    <Notes></Notes>
    <ID>11f13e2e-ae97-45b5-a9a9-23fa7f6bb767</ID>
    <ID>b22834c0-a570-4e6b-97c3-5067a14d118d</ID>
    <ID>ed458593-5e1a-4dc1-94f0-a66eeef2dd79</ID>
    <ID>d25584a9-1db2-48cf-9a70-9b81e5a7e7f2</ID>
    <LogisticalInfo>
        <BaseUnit></BaseUnit>
        <Compoundheight>4.78</Compoundheight>
        <Compoundwidth>5.67</Compoundwidth>
        <Compounddepth></Compounddepth>
        <Compoundweight></Compoundweight>
        <CompoundweightUnit>g</CompoundweightUnit>
        <TotalHeight>30.5</TotalHeight>
        <Totalwidth>542.7</Totalwidth>
        <Totaldepth>37.5</Totaldepth>
        <TotalWeight>2840</TotalWeight>
        <height>mm</height>
        <Weight>g</Weight>
        <Depth>mm</Depth>
    </LogisticalInfo>
</Product>

I would like to extract Compoundheight and Compounddepth, Below is the part of code to extract it but it is throwing error:java.lang.StringIndexOutOfBoundsException.

 stringXmlDocument = productHeader + toStringXml(node, true) + productTrailer;

int CompoundheightCodeStart = stringXmlDocument.indexOf("<Compoundheight>");
int CompoundheightCodeEnd = stringXmlDocument.indexOf("</Compoundheight>"); height_packed=Double.parseDouble(stringXmlDocument.substring(CompoundheightCodeStart+13,CompoundheightCodeEnd));
int CompounddepthCodeStart = stringXmlDocument.indexOf("<Compounddepth>");
int CompounddepthCodeEnd = stringXmlDocument.indexOf("</Compounddepth>");depth_packed=Double.parseDouble(stringXmlDocument.substring(CompounddepthCodeStart+12, CompounddepthCodeEnd));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A better approach than indexOf would be to use Java XPath implementation for navigating XML.

See the Java XPath implementation, and the XPath Specification.

An example:

    // parse the xml into a Document
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputStream inputStream = this.class.getResourceAsStream("test.xml");
    Document document = builder.parse(inputStream);

    // Obtain a specific element from within the Document
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    String articleName = xPath.evaluate("/Stock/Product/ArticleName", document);
    System.out.println("ArticleName is: " + articleName);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...