Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Into XML 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
I'm starting to get into XML, so I was wondering if there is a good primer on the topic - I'm not really finding anything good or even halfway sensible.

That said, I'm trying to conduct a search against a XML file I've created, but I'm finding it's not working on anything I try, any example I try off the net. So basically I'm just looping through all the nodes and picking out the ones with the right values: if this is what I have to do, then the whole thing is pretty brain-dead.

I'm trying to use SelectSingleNode with a XPath string.

A section to illustrate what I'm trying to do.
Code:
<MainNode Attrib1="Value1">
- <Child1 Attrib2="Value2">
  - <GrandChild1 Attrib3="Value3">
- <Child2 Attrib2="Value4">
  - <GrandChild1 Attrib3="Value5">

What I'm needing is to find a specific "Child" node that has a "Attrib2" value of a specific thing.

Any thoughts?


It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
How are you managing/handling the XML document? Are you using DOM or another component/library?
 
using DOM (IXMLDOMDocument).

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
It looks like SketchPath might be a useful tool to help.

Which promptly redirects to the version that is not free.

Anyhow, I'm starting to wonder whether I got the XML file created properly or not. I tried just looping on the child nodes and came back with the same error as before. So am I creating the child nodes in a way that is invalid?

The original thing I was trying to do was isolate a child node and then report all its children.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Glenn,

XML is in fact wellformed HTML so you need closing tags for each opening tag. I Use XML notepad (free tool from M$):


so for your XML doc:

Code:
<MainNode Attrib1="Value1">
  <Child1 Attrib2="Value2">
   <GrandChild1 Attrib3="Value3" />
  </Child1> 
  <Child2 Attrib2="Value4">
    <GrandChild1 Attrib3="Value5" />
  </Child2>
</MainNode>

check out the XML databinding wizard (available from D7 if I'm not mistaken)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
XML is in fact wellformed HTML so you need closing tags for each opening tag.

Which I do since I'm using program-generated XML files in trying to do this query. I was just trying to simplify things without posting 200K worth of XML (overall).

That said, I did another section of my code that accesses an XML file with a simpler structure (one level deep) and got it to work right. And I got the XML file diagrammed above to work with just "//Child", but when I try "//Child[@Attrib='Value']" as I did in the simpler XML, it returns no elements even though 'Value' is the first 'Attrib' value in the file.

So have I got to always search the low-level elements to get it to work right (assuming it IS supposed to work right)?

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
I got it working. Don't know if it's the best way, but found a way. I'd definitely like to know if there's a more clearer way to do it.

Code:
function FindNode(Path: String): IXMLDOMNode;
var
  nodelist: IXMLDOMNodeList;
  node: IXMLDOMElement;
  nodemap: IXMLDOMNamedNodeMap;
  basenode: IXMLDOMNode;
begin
  nodelist := doc.selectNodes('//FilePath');
  if nodelist <> nil then
    begin
      node := NodeList.NextNode as IXMLDOMElement;
      while node <> nil do
        begin
          nodemap := node.attributes;
          basenode := nodemap.getNamedItem('Path');
          if basenode.text = Path then break;
          node := NodeList.NextNode as IXMLDOMElement;
        end;
    end;
// return node belonging to attribute & not attribute itself (basenode)
  Result := node;  
end;

procedure THashCheck.FillList(basenode: IXMLDOMNode; FileList, FileValues: TStringList);
var
  nodelist: IXMLDOMNodeList;
  node: IXMLDOMElement;
  outstr: string;
begin
  nodelist := basenode.Get_childNodes;
  // iterate and write node contents here
end;

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top